package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.model.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class UserDaoImpl implements UserDao {
    public User login(String username, String password) {
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
        User user = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/qf?useSSL=false&characterEncoding=utf-8", "root", "123456");
            String sql = "select * from tb_user where username = ? and password = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            rs = ps.executeQuery();
            while (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setMoney(rs.getDouble("money"));
                user.setSex(rs.getInt("sex"));
                user.setPassword(rs.getString("password"));
                user.setUsername(rs.getString("username"));
                user.setCreateTime(rs.getDate("create_time"));
                user.setPhone(rs.getString("phone"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                rs.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    public ArrayList<User> findAll() {
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
        ArrayList<User> list = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/qf?useSSL=false&characterEncoding=utf-8", "root", "123456");
            String sql = "select * from tb_user";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setMoney(rs.getDouble("money"));
                user.setSex(rs.getInt("sex"));
                user.setPassword(rs.getString("password"));
                user.setUsername(rs.getString("username"));
                user.setCreateTime(rs.getDate("create_time"));
                user.setPhone(rs.getString("phone"));
                list.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                rs.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    public void del(int id) {
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/qf?useSSL=false&characterEncoding=utf-8", "root", "123456");
            String sql = "delete from tb_user where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public boolean regist(User user) {
        PreparedStatement ps = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/qf?useSSL=false&characterEncoding=utf-8", "root", "123456");
            String sql = "insert into tb_user (username,password,phone,create_time,money,sex)values (?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getPhone());
            ps.setDate(4, new java.sql.Date(user.getCreateTime().getTime()));
            ps.setDouble(5, user.getMoney());
            ps.setInt(6, user.getSex());
            int i = ps.executeUpdate();
            return i > 0 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}


